package com.example.secondhandshop.model;

import android.os.NetworkOnMainThreadException;
import android.util.Log;

import com.example.secondhandshop.bean.AllchatitemData;
import com.example.secondhandshop.bean.Allchatuser;
import com.example.secondhandshop.bean.Chatitem;
import com.example.secondhandshop.bean.ResponseBody;
import com.example.secondhandshop.ui.activity.Constans;
import com.example.secondhandshop.ui.activity.LoginAcitivity;
import com.example.secondhandshop.ui.activity.Login_back;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import com.google.gson.JsonSyntaxException;

public class RechargeModel {
    private OkHttpClient client = new OkHttpClient();
    private final Gson gson = new Gson();
    private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
    public void recharge(Integer tranMoney, long userId, OnRechargeListener listener) {
        String url = String.format(Constans.SERVER_URL()+"tran/goods/recharge?tranMoney=%d&userId=%d", tranMoney, userId);
        Headers headers = new Headers.Builder()
                .add("Accept", "application/json, text/plain, */*")
                .add("appId", Constans.getAppId())
                .add("appSecret", Constans.getAppSecret())
                .add("Content-Type", "application/json")
                .build();
        Request request = new Request.Builder()
                .url(url)
                .headers(headers)
                .post(RequestBody.create(MEDIA_TYPE_JSON, ""))
                .build();

        Log.d("Recharge API Request", request.toString()); // 输出完整请求
        try {
            //发起请求，传入callback进行回调
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                    listener.onError();
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Log.d("RechargeModel", "onResponse: " + response);
                    System.out.println(response.body());
                    System.out.println("response.isSuccessful() is"+response.isSuccessful());
                    if (response.isSuccessful()) {
                        String responseBody = response.body().string();
                        Gson gson = new Gson();
                        try {
                            JsonObject jsonResponse = JsonParser.parseString(responseBody).getAsJsonObject();
                            System.out.println(jsonResponse);
                            int code = jsonResponse.get("code").getAsInt();
                            if (code == 200) {
                                listener.onSuccess();
                            } else {
                                listener.onFailure();
                            }
                        } catch (JsonSyntaxException e) {
                            listener.onError();
                        }
                    } else {
                        listener.onError();
                    }
                }
            });
        } catch (NetworkOnMainThreadException ex) {
            ex.printStackTrace();
            listener.onError();
        }
    }

    public interface OnRechargeListener {
        void onSuccess();
        void onFailure();
        void onError();
    }
}